/*
 * 文件名: MenuActivity.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XIAOFANQING001
 * 创建时间: 2012-1-16
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package com.longevityassistant.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.longevityassistant.menu.CommonMenuUtil;
import com.longevityassistant.menu.CommonMenuUtil.DefaultMenuType;
import com.longevityassistant.menu.MenuDialog;
import com.longevityassistant.menu.MenuItem;
import com.longevityassistant.menu.ShowMenu;

/**
 * 该Activity处理Menu物理键,当用户点击Menu键的时,会弹出一个 定制的Menu
 * @author EX-XIAOFANQING001
 * @date 2012-1-16
 * @version [Android PABank C01, @2012-1-16]
 * @description
 */
public abstract class MenuActivity extends ActivityGroup implements ShowMenu
{
	
    /**
     * 弹出框
     */
    private MenuDialog mMenuDialog;
    
    /**
     * Menu数据
     */
    private List<MenuItem> mMenus;
    
    /**
	 * 首页TAB索引 
	 * 详情请见 #HomeTabConstants
	 */
	public static int mHomeTabIndex = HomeTabConstants.TAB_WEALTH;
	
	/**
	 * 主界面TAB索引常量类
	 * @author EX-LUOCHUN001
	 *
	 */
	public static class HomeTabConstants{
		public static int TAB_WEALTH = 1;
	}
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initMenu();
    }

    @Override
    protected void onDestroy()
    {
        hideSafeSoftKeyboard();
        super.onDestroy();
    }

    /**
     * 自己设置默认的菜单项目
     * @param dmt
     * @return
     */
    public void setMenus(DefaultMenuType... dmts) {
        if (dmts == null || dmts.length ==0 ) {
            return ;
        } else {
            List<MenuItem> menus = new ArrayList<MenuItem>();
            for (int i=0; i<dmts.length; i++) {
                DefaultMenuType dmt = dmts[i];
                MenuItem mi = (MenuItem) CommonMenuUtil.createSingleDefaultMenu(this, dmt);
                menus.add(mi);
            }
            mMenus = menus;
        }
    }
    
    /**
     * 自己设置默认的菜单项目
     * @param dmt
     * @return
     */
    public void setMenus(List<MenuItem> menuItems) {
        mMenus = menuItems;
    }
    
    /**
     * 取消自定义菜单
     */
    public void cancelMenu() {
        mMenus = null;
    }
    
    private void initMenu() {
        mMenus = CommonMenuUtil.getDefaultMenus(this);
        mMenuDialog = new MenuDialog(this);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
            if (mMenuDialog.isShowing()) {
                mMenuDialog.dismiss();
            } else {
                showMenu();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showMenu()
    {
        hideSafeSoftKeyboard();
        
        if (mMenus != null && !mMenus.isEmpty()) {
            mMenuDialog.setMenus(mMenus);
            mMenuDialog.show();
        }
    }
    
    protected void hideSafeSoftKeyboard() {
        
    }
    
    
    private TigerDialog tigerDialog ;

    /**
     * 启动小老虎动画
     * @param context
     */
    public void showTiger(Context context) {
        showTiger(context, null, false);
    }

    /**
     * 启动小老虎动画,且用户不能取消动画
     * @param context
     * @param message
     */
    public void showTiger(Context context, final String message) {
        showTiger(context, message, true);
    }
    
    private void showTiger(Context context, String message, boolean canNotCancel) {
        if (tigerDialog != null && tigerDialog.isShowing()) {
            return;
        }
        
        tigerDialog = new TigerDialog(context, canNotCancel, message);
        tigerDialog.show();
    }


    public void dismissTiger(Context context) {
        if (tigerDialog != null) {
            tigerDialog.dismiss();
            tigerDialog = null;
        }
    }
    
    /**
     * 小老虎动画类
     * @author EX-XIAOFANQING001
     * @version [Android PABank C01, 2012-3-21]
     */
    static class TigerDialog extends Dialog {
        
        /**
         * 小老虎的动画对象
         */
        private AnimationDrawable tigerAnimation;
        
        private Handler handler = new Handler();
        
        private boolean canNotCancel ;
        
        private String tipMsg ;

        public  TigerDialog(final Context ctx, boolean canNotCancel, String tipMsg) {
            super(ctx);
            
            this.canNotCancel = canNotCancel;
            this.tipMsg = tipMsg;
            
            this.getContext().setTheme(android.R.style.Theme_InputMethod);
            
            setContentView(R.layout.tiger_dialog);
            
            final ImageView ivTiger = (ImageView) findViewById(R.id.tiger);
        
            
            // 小老虎弹出后背景变灰
            Window window  = getWindow();
            WindowManager.LayoutParams attributesParams = window.getAttributes();
            attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
            attributesParams.dimAmount = 0.5f;
            
            window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
            
            // 延迟300毫秒启动动画
            handler.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    ivTiger.setBackgroundResource(R.anim.tiger_dialog);
                    tigerAnimation = (AnimationDrawable) ivTiger.getBackground();
                    tigerAnimation.start();
                }
            }, 400);
        }
        
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (canNotCancel) {
                    Toast.makeText(getContext(), tipMsg, Toast.LENGTH_SHORT).show();
                    return true;
                }
            }
            return super.onKeyDown(keyCode, event);
        }
    }
}

